package com.struts2.example.action;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.struts2.example.model.Answer;
import com.struts2.example.model.Connect;
import com.struts2.example.model.Question;
import com.struts2.example.model.StructTest;
import com.struts2.example.model.TestInformation;

public class TestAction extends ActionSupport {
	private String TAG="TestAction:";
	private static final long serialVersionUID = 1L;
	private Answer answer;
	private List<Answer> listAnswer;
	private Question questionBean;
	private TestInformation testInformation;
	private StructTest structTest;
    private List<StructTest> listStructTest;
    
	public String execute() {		
		System.out.print(TAG+testInformation.getNumQuestion());
		listStructTest=new ArrayList<StructTest>();
		
		String ret = ERROR;
		Connect conn = new Connect();
		Connection connected = conn.Connected();
		
		try {
			String sql = "SELECT * FROM testonline_bang_dai.question WHERE question_id < ?";
			PreparedStatement ps = connected.prepareStatement(sql);
			ps.setInt(1,testInformation.getNumQuestion()+1);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				listAnswer=new ArrayList<Answer>();
				String sqlSub = "SELECT * FROM testonline_bang_dai.answer WHERE question_id = ?";
				PreparedStatement psSub = connected.prepareStatement(sqlSub);
				psSub.setInt(1,rs.getInt(1));
				ResultSet rsSub = psSub.executeQuery();
				while (rsSub.next()) {
					answer=new Answer();
					answer.setAnswerId(rsSub.getInt(1));
                    answer.setAnswerText(rsSub.getString(3));
					listAnswer.add(answer);
					
				}
				questionBean=new Question();
				questionBean.setQuestionId(rs.getInt(1));
				questionBean.setQuestionText(rs.getString(2));
				structTest=new StructTest();
				structTest.setQuestion(questionBean);
				structTest.setListAnswer(listAnswer);
				listStructTest.add(structTest);
				ret = SUCCESS;
				
			}
			
			
		} catch (Exception e) {
			ret = "ERROR";
		} finally {
			if (connected != null) {
				try {
					connected.close();
				} catch (Exception e) {
				}
			}
		}
		return ret;
	}
	
	//getter and setter for questionBean
	public Question getQuestionBean() {
		return questionBean;
	}

	public void setQuestionBean(Question questionBean) {
		this.questionBean = questionBean;
	}
	
	//getter and setter for answer;
	
	public Answer getAnswer(){
		return this.answer;
	}

	public void setAnswer(Answer answer) {
		this.answer=answer;
	}
	
	//getter and setter for listAnswer;
	
	public List<Answer> getListAnswer(){
		return this.listAnswer;
	}

	public void setListAnswer(List<Answer> listAnswer) {
		this.listAnswer=listAnswer;
	}
	
	//getter and setter for testInformation;

	public TestInformation getTestInformation()
	{
		return this.testInformation;
	}
	
	public void setTestInformation(TestInformation testInformation)
	{
		this.testInformation=testInformation;
	}
	
	
	public StructTest getStructTest()
	{
		return structTest;
	}
	
	public void setStructTest(StructTest structTest)
	{
		this.structTest=structTest;
	}

	public List<StructTest> getListStructTest()
	{
		return this.listStructTest;
	}
	
	public void setListStructTest(List<StructTest> listStructTest)
	{
		this.listStructTest=listStructTest;
	}
	
}
